const XLSX = require('node-xlsx');
const fs = require('fs')
const jsyaml = require('js-yaml');
const path = require('path')
const currentDir = path.resolve(__dirname);

const folderName = 'xlsl'; // 替换为你的文件夹名称
const folderPath = path.join(currentDir, folderName);
fs.readdir(folderPath, (err, files) => {
    if (err) {
        throw err;
    }
    let outyaml = {
        locale: "zh-Hans-CN"
    };
    let out2yaml = { locale: "index" };
    outyaml["items"] = {};
    out2yaml["items"] = {};
    files.forEach(file => {
        console.log('读取的配置表文件名', file);

        const workbook = fs.readFileSync(folderPath + '/' + file);
        const jsonObj = XLSX.parse(workbook);
        for (let index = 0; index < jsonObj.length; index++) {
            const element = jsonObj[index];

            for (let i = 2; i < element.data.length; i++) {
                const element1 = element.data[i];
                console.log("element", element1)

                let key = element1[0];

                outyaml["items"][key] = {
                    key,
                    value: element1[1],
                    type: 4,
                    isVariant: false,
                    associations: [],
                    _variants: []
                }
                out2yaml["items"][key] = {
                    key,
                    value: "",
                    type: 4,
                    isVariant: false,
                    associations: [],
                    _variants: []
                }
            }
        }

    })

    outyaml["languageConfig"] = {
        bcp47Tag: "zh-Hans-CN",
        translateFinished: 0,
        translateTotal: 0,
        compileFinished: 0,
        compileTotal: 2265,
    };
    out2yaml["languageConfig"] = {
        bcp47Tag: "index",
        translateFinished: 0,
        translateTotal: 0,
        compileFinished: 0,
        compileTotal: 0,
    }

    const yamlStr = jsyaml.dump(outyaml);
    const yamlStr2 = jsyaml.dump(out2yaml);

    // 输出YAML字符串或将其保存到文件
    console.log(yamlStr);
    fs.writeFileSync('zh-Hans-CN.yaml', yamlStr);
    fs.writeFileSync('index.yaml', yamlStr2);
})

// 读取XLSX文件
// const workbook = fs.readFileSync('zh-Hans-CN.xlsx');
// const worksheet = workbook.Sheets[workbook.SheetNames[0]];


// 将工作表转换为JSON对象
// const jsonObj = XLSX.parse(workbook);
// for (let index = 0; index < jsonObj.length; index++) {
//     const element = jsonObj[index];

//     for (let i = 2; i < element.data.length; i++) {
//         const element1 = element.data[i];
//         console.log("element", element1)

//         let key = element1[0];
//         outyaml[key] = {
//             key,
//             value: element1[1],
//             type: 4,
//             isVariant: false,
//             associations: [],
//             _variants: []
//         }
//         out2yaml[key] = {
//             key,
//             value: "",
//             type: 4,
//             isVariant: false,
//             associations: [],
//             _variants: []
//         }
//     }
// }
// 将JSON对象转换为YAML字符串
// const yamlStr = jsyaml.dump(outyaml);
// const yamlStr2 = jsyaml.dump(out2yaml);

// // 输出YAML字符串或将其保存到文件
// console.log(yamlStr);
// fs.writeFileSync('zh-Hans-CN.yaml', yamlStr);
// fs.writeFileSync('index.yaml', yamlStr2);